/* eslint-disable vue/require-default-prop */
/* eslint-disable vue/require-default-prop */
<template>
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="showDiolog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addDept" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item
        label="部门编码"
        prop="code"
      >>
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item
        label="部门负责人"
        prop="manager"
      >>
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
          @blur="checkManager"
        >
          <el-option v-for="item in EmployeeList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item
        label="部门介绍"
        prop="introduce"
      >>
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'

export default {
  props: {
    showDiolog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      require: true,
      default: null
    }
  },
  data() {
    // 自定义校验函数
    const checkNameRepeat = async(rule, value, callback) => {
      // 拿到所有部门的列表
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        // 编辑
        isRepeat = depts
          .filter((item) => item.pid === this.treeNode.pid && item.id !== this.treeNode.id)
          .some((item) => item.name === value)
      } else {
        // 找到点击的父部门包含的子部门数组  用some判断是否和用户输入的值重名
        isRepeat = depts
          .filter((item) => item.pid === this.treeNode.id)
          .some((item) => item.name === value)
      }

      isRepeat ? callback(new Error('同部门下不能重名')) : callback()
      // if (isRepeat) {
      //   callback(new Error('同部门下不能重名'))
      // } else {
      //   callback()
      // }
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      // 拿到所有部门的列表
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        // 编辑
        isRepeat = depts.some((item) => item.code === value && item.id !== this.treeNode.id)
      } else {
        // 新增
        isRepeat = depts.some((item) => item.code === value)
      }
      isRepeat ? callback(new Error('部门编码不能重名')) : callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          { trigger: 'blur', validator: checkNameRepeat }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          { trigger: 'blur', validator: checkCodeRepeat }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-50个字符',
            trigger: 'blur'
          }
        ]
      },
      EmployeeList: []
    }
  },
  methods: {
    async getEmployeeSimple() {
      this.EmployeeList = await getEmployeeSimple()
      // console.log(this.EmployeeList)
    },
    checkManager() {
      setTimeout(() => {
        this.$refs.addDept.validateField('manager')
      }, 200)
    },
    // 点击确认
    async btnOk() {
      // 表单验证
      await this.$refs.addDept.validate()
      // 发送请求
      if (this.formData.id) {
        // 编辑
        await updateDepartments(this.formData)
      } else {
        // 新增
        await addDepartments({ ...this.formData, pid: this.treeNode.id })
      }

      // 提示用户
      this.$message.success('添加成功')
      // 关闭弹窗
      this.$emit('update:showDiolog', false)
      // 渲染页面
      this.$emit('addelDepartment')
    },
    // 点击取消
    btnCancel() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 清理表单绑定的数据  在清理校验结果
      this.$refs.addDept.resetFields()
      // 关闭弹窗
      this.$emit('update:showDiolog', false)
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style>
</style>
